package com.happly.payHapplycloud.service.impl;

import com.happly.payHapplycloud.common.Const;
import com.happly.payHapplycloud.dao.OrderMapper;
import com.happly.payHapplycloud.enums.PayPlatformEnum;
import com.happly.payHapplycloud.pojo.Order;
import com.happly.payHapplycloud.service.IOrderPayService;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class OrderPayServiceImpl implements IOrderPayService {

    @Autowired
    private BestPayService bestPayService;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PayResponse createOrder(String orderId,BigDecimal amount,BestPayTypeEnum bestPayTypeEnum) {
        //写入数据库
        Order order = new Order();
        order.setOrderNo(Long.parseLong(orderId));
        order.setPayment(amount);
        order.setPaymentType(1);
        orderMapper.insertSelective(order);

        PayRequest request = new PayRequest();
        request.setOrderName("5498955-最好的支付sdk");
        request.setOrderId(orderId);
        request.setOrderAmount(amount.doubleValue());
        request.setPayTypeEnum(bestPayTypeEnum);

        PayResponse response = bestPayService.pay(request);
        log.info("发起支付",response);
        return response;
    }

    @Override
    public void asyncNotify(String notifyData) {
        //1.签名校验
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("payRespomse = {}",payResponse);
    }

}
